package com.fast.develop.api.rest;

import com.fast.develop.api.DomainModelFastDevelopConfiguration;
import com.fast.develop.api.BatchDto;
import com.fast.develop.core.ModuleContextFactory;
import com.fast.develop.core.persistence.BatchObject;
import com.fast.develop.core.persistence.DomainModelManager;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author 莫寅
 * @createTime 2022-06-08 23:25
 * @desc：
 */
@RequestMapping("meta")
public class RuntimeRest {

    private DomainModelFastDevelopConfiguration domainAutoConfiguration;
    private ModuleContextFactory moduleContextFactory;

    public RuntimeRest(DomainModelFastDevelopConfiguration domainAutoConfiguration, ModuleContextFactory moduleContextFactory) {
        this.domainAutoConfiguration = domainAutoConfiguration;
        this.moduleContextFactory = moduleContextFactory;
    }

    @PostMapping("configuration/reload")
    public ResponseEntity reload(){
        domainAutoConfiguration.reload();
        return new ResponseEntity(HttpStatus.OK);
    }


    @PostMapping("{model}/batch")
    public ResponseEntity<BatchDto> batch(@PathVariable(name = "model") String model, @RequestBody BatchObject batchObject){
        DomainModelManager modelManager = moduleContextFactory.getDomainModelManager();

        return new ResponseEntity(modelManager.batch(model, batchObject), HttpStatus.OK);
    }
}
